using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Media.Imaging;
namespace MicroMsg.Manager
{
	public class HeadImageMgr
	{
		private enum HeadImgStatus
		{
			pending,
			downloading,
			decodeFail,
			noImg,
			netError,
			hasImg
		}
		public class CleanThreadParam
		{
			public string strUsrName;
			public string path;
			public int index;
		}
		private const string TAG = "HeadImageMgr";
		private const string lastCleanTimeStamp = "HeadImageMgrCleanHeadImg";
		private const string lastCleanDirIndex = "HeadImageMgrCleanDirIndex";
		private const bool tryContinue = true;
		private const bool tryEnd = false;
		private const int hashDirNum = 26;
		private const int maxHeadImgCacheNum = 250;
		private const int disposeHeadImgNum = 100;
		private const string FileExt = ".jpg";
		private static string[] ResHeadImageUserNames = new string[]
		{
			"weixin",
			"fmessage",
			"qmessage",
			"floatbottle",
			"masssend",
			"newsapp",
			"blogapp",
			"qqmail",
			"filehelper"
		};
		private static byte[] versionTag = new byte[]
		{
			118,
			101,
			114,
			115,
			105,
			111,
			110
		};
		private static StorageHash hasher = new StorageHash();
		private static Dictionary<string, BitmapImage> mapHeadImg = new Dictionary<string, BitmapImage>();
		private static Dictionary<string, HeadImageMgr.HeadImgStatus> mapHeadImgStatus = new Dictionary<string, HeadImageMgr.HeadImgStatus>();
		private static Dictionary<getImageCallBack, List<string>> mapHeadImgCallBack = new Dictionary<getImageCallBack, List<string>>();
		private static List<string> downLoadRequestList = new List<string>();
		private static Dictionary<string, int> mapHeadImgVersion = new Dictionary<string, int>();
		private static Dictionary<string, int> mapHeadImgLocalVersion = new Dictionary<string, int>();
		private static BitmapImage _defaultHeadImage;
		private static BitmapImage _defaultQQHeadImage;
		private static List<getImageCallBack> cbList = new List<getImageCallBack>();
		public static BitmapImage defaultHeadImage
		{
			get
			{
				if (HeadImageMgr._defaultHeadImage != null)
				{
					return HeadImageMgr._defaultHeadImage;
				}
				HeadImageMgr._defaultHeadImage = ImageCacheMgr.loadFromRes("Resource/Img/CommonUse/DefaultHead.png");
				return HeadImageMgr._defaultHeadImage;
			}
		}
		public static BitmapImage defaultQQHeadImage
		{
			get
			{
				if (HeadImageMgr._defaultQQHeadImage != null)
				{
					return HeadImageMgr._defaultQQHeadImage;
				}
				HeadImageMgr._defaultQQHeadImage = ImageCacheMgr.loadFromRes("Resource/Img/CommonUse/default_qq_avatar.png");
				return HeadImageMgr._defaultQQHeadImage;
			}
		}
		public static void init()
		{
			AccountMgr.registerLoginNotify(new onAccountLoginCallback(HeadImageMgr.onLogin));
			EventCenter.registerEventHandler(EventConst.ON_NETSCENE_BATCHGETHEADIMG_ERR, new EventHandlerDelegate(HeadImageMgr.onNetSceneGetImageError));
		}
		public static void clearOldHeadImage()
		{
			if (!AccountMgr.isLogin)
			{
				return;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					if (!userStoreForApplication.DirectoryExists(StorageIO.getHeadImgQQPath()))
					{
						return;
					}
					StorageIO.deleteDir(userStoreForApplication, StorageIO.getHeadImgQQPath(), true);
					string headImgPath = StorageIO.getHeadImgPath();
					string[] fileNames = userStoreForApplication.GetFileNames(headImgPath + "/*");
					string[] array = fileNames;
					for (int i = 0; i < array.Length; i++)
					{
						string text = array[i];
						if (text.get_Length() > 4)
						{
							string text2 = text.Substring(0, text.get_Length() - 4);
							if (!(text2 == AccountMgr.strUsrName) && !HeadImageMgr.isContact(text2))
							{
								userStoreForApplication.DeleteFile(headImgPath + "/" + text);
							}
						}
					}
				}
				StorageMgr.chatMsg.patch_mov_qq_msg();
			}
			catch (Exception ex)
			{
				Log.smethod_1("HeadImageMgr", "cleanDir fail " + ex);
			}
		}
		private static void onLogin()
		{
			HeadImageMgr.hasher.setParam(26, StorageIO.getHeadImgTempPath());
			HeadImageMgr.hasher.fileExt = ".jpg";
			HeadImageMgr.clearCache();
			HeadImageMgr.mapHeadImgStatus.Clear();
			HeadImageMgr.mapHeadImgCallBack.Clear();
			HeadImageMgr.downLoadRequestList.Clear();
			HeadImageMgr.mapHeadImgVersion.Clear();
			HeadImageMgr.mapHeadImgLocalVersion.Clear();
		}
		private static void onNetSceneGetImageError(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null || evtArgs.mObject == null)
			{
				return;
			}
			List<string> list = evtArgs.mObject as List<string>;
			if (list != null && list.get_Count() > 0)
			{
				using (List<string>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string current = enumerator.get_Current();
						if (HeadImageMgr.mapHeadImgStatus.ContainsKey(current))
						{
							HeadImageMgr.mapHeadImgStatus.set_Item(current, HeadImageMgr.HeadImgStatus.netError);
							Log.smethod_5("HeadImageMgr", "download  image net error " + current);
						}
					}
				}
				return;
			}
		}
		private static void addCache(string strUserName, BitmapImage bitmapImage_0)
		{
			if (bitmapImage_0 != null && !string.IsNullOrEmpty(strUserName))
			{
				if (HeadImageMgr.mapHeadImg.get_Count() > 250)
				{
					List<string> list = Enumerable.ToList<string>(Enumerable.Select<KeyValuePair<string, BitmapImage>, string>(HeadImageMgr.mapHeadImg, (KeyValuePair<string, BitmapImage> keyValuePair_0) => keyValuePair_0.get_Key()));
					int num = 0;
					using (List<string>.Enumerator enumerator = list.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							string current = enumerator.get_Current();
							if (!(current == AccountMgr.curUserName) && !Enumerable.Contains<string>(HeadImageMgr.ResHeadImageUserNames, current))
							{
								HeadImageMgr.mapHeadImg.Remove(current);
								num++;
								if (num > 100)
								{
									break;
								}
							}
						}
					}
				}
				HeadImageMgr.mapHeadImg.set_Item(strUserName, bitmapImage_0);
				return;
			}
		}
		private static void clearCache()
		{
			List<string> list = Enumerable.ToList<string>(Enumerable.Select<KeyValuePair<string, BitmapImage>, string>(HeadImageMgr.mapHeadImg, (KeyValuePair<string, BitmapImage> keyValuePair_0) => keyValuePair_0.get_Key()));
			using (List<string>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (!(current == AccountMgr.curUserName) && !Enumerable.Contains<string>(HeadImageMgr.ResHeadImageUserNames, current))
					{
						HeadImageMgr.mapHeadImg.Remove(current);
					}
				}
			}
		}
		private static bool tryGetCahceHeadImage(string strUserName, out BitmapImage bitmapImage_0)
		{
			bitmapImage_0 = null;
			if (!HeadImageMgr.mapHeadImg.ContainsKey(strUserName))
			{
				return true;
			}
			bitmapImage_0 = HeadImageMgr.mapHeadImg.get_Item(strUserName);
			return false;
		}
		private static bool tryGetAccountHeadImage(string strUserName, out BitmapImage bitmapImage_0)
		{
			bitmapImage_0 = null;
			if (strUserName != AccountMgr.strUsrName)
			{
				return true;
			}
			if (!AccountMgr.hasHeadImg)
			{
				HeadImageMgr.mapHeadImgStatus.set_Item(strUserName, HeadImageMgr.HeadImgStatus.noImg);
				return false;
			}
			byte[] array = HeadImageMgr.readHeadImg(strUserName);
			if (array == null)
			{
				Log.smethod_1("HeadImageMgr", "error no exist account img" + strUserName);
				return false;
			}
			bitmapImage_0 = ImageCacheMgr.loadFromBuf(array);
			return false;
		}
		public static bool isResHeadImage(string strUserName)
		{
			return Enumerable.Contains<string>(HeadImageMgr.ResHeadImageUserNames, strUserName);
		}
		private static bool tryGetResHeadImage(string strUserName, out BitmapImage bitmapImage_0)
		{
			bitmapImage_0 = null;
			if (!Enumerable.Contains<string>(HeadImageMgr.ResHeadImageUserNames, strUserName))
			{
				return true;
			}
			if (MMTheme.getSystemThemeType() == MMThemeTypes.COOL_BLACK_THEME)
			{
				bitmapImage_0 = ImageCacheMgr.loadFromRes(string.Format("Resource/Img/CommonUse/SystemHeadImgs/PluginHead_{0}.png", strUserName));
			}
			else
			{
				bitmapImage_0 = ImageCacheMgr.loadFromRes(string.Format("Resource/Img/CommonUse/SystemHeadImgs/PluginHead_{0}_light.png", strUserName));
			}
			if (bitmapImage_0 == null)
			{
				DebugEx.debugBreak();
			}
			return false;
		}
		private static bool isContact(string strUserName)
		{
			return !ContactHelper.isQContact(strUserName) && !ContactHelper.isBottleContact(strUserName) && (strUserName == AccountMgr.strUsrName || null != StorageMgr.contact.method_0(strUserName));
		}
		private static bool tryGetContactHeadImage(string strUserName, out BitmapImage bitmapImage_0)
		{
			bitmapImage_0 = null;
			if (!HeadImageMgr.isContact(strUserName))
			{
				return true;
			}
			Contact contact = StorageMgr.contact.method_0(strUserName);
			if (contact.nVerifyFlag != 0u)
			{
				return true;
			}
			switch (contact.nImgFlag)
			{
			case 1u:
			case 2u:
			case 3u:
				IL_42:
				bitmapImage_0 = ImageCacheMgr.loadFromBuf(HeadImageMgr.readHeadImg(strUserName));
				return false;
			case 4u:
				HeadImageMgr.mapHeadImgStatus.set_Item(strUserName, HeadImageMgr.HeadImgStatus.noImg);
				return false;
			}
			goto IL_42;
		}
		private static bool tryGetHashHeadImage(string strUserName, out BitmapImage bitmapImage_0)
		{
			byte[] array = HeadImageMgr.readHeadImg(strUserName);
			bitmapImage_0 = ImageCacheMgr.loadFromBuf(array);
			if (bitmapImage_0 != null && HeadImageMgr.mapHeadImgVersion.ContainsKey(strUserName))
			{
				HeadImageMgr.mapHeadImgLocalVersion.set_Item(strUserName, HeadImageMgr.getHeadImgVersion(array));
			}
			return false;
		}
		public static BitmapImage getCacheHeadImage(string strUserName)
		{
			if (string.IsNullOrEmpty(strUserName))
			{
				return null;
			}
			BitmapImage bitmapImage = null;
			HeadImageMgr.tryGetCahceHeadImage(strUserName, out bitmapImage);
			if (bitmapImage == null && HeadImageMgr.isResHeadImage(strUserName))
			{
				return HeadImageMgr.getHeadImage(strUserName);
			}
			return bitmapImage;
		}
		public static BitmapImage getHeadImage(string strUserName)
		{
			if (string.IsNullOrEmpty(strUserName))
			{
				return null;
			}
			BitmapImage bitmapImage = null;
			while (HeadImageMgr.tryGetCahceHeadImage(strUserName, out bitmapImage))
			{
				if (!HeadImageMgr.tryGetAccountHeadImage(strUserName, out bitmapImage) || !HeadImageMgr.tryGetResHeadImage(strUserName, out bitmapImage) || !HeadImageMgr.tryGetContactHeadImage(strUserName, out bitmapImage) || !HeadImageMgr.tryGetHashHeadImage(strUserName, out bitmapImage))
				{
					HeadImageMgr.addCache(strUserName, bitmapImage);
					return bitmapImage;
				}
			}
			return bitmapImage;
		}
		public static bool downLoadHeadImage(string strUserName, getImageCallBack getImageCallBack_0 = null)
		{
			if (string.IsNullOrEmpty(strUserName))
			{
				return false;
			}
			if (getImageCallBack_0 == null)
			{
				getImageCallBack_0 = new getImageCallBack(HeadImageMgr.getImageBroadCasting);
			}
			switch (HeadImageMgr.mapHeadImgStatus.ContainsKey(strUserName) ? HeadImageMgr.mapHeadImgStatus.get_Item(strUserName) : HeadImageMgr.HeadImgStatus.pending)
			{
			case HeadImageMgr.HeadImgStatus.pending:
			case HeadImageMgr.HeadImgStatus.netError:
			case HeadImageMgr.HeadImgStatus.hasImg:
				HeadImageMgr.mapHeadImgStatus.set_Item(strUserName, HeadImageMgr.HeadImgStatus.downloading);
				HeadImageMgr.downLoadRequest(strUserName);
				HeadImageMgr.addCallback(getImageCallBack_0, strUserName);
				return true;
			case HeadImageMgr.HeadImgStatus.downloading:
			case HeadImageMgr.HeadImgStatus.decodeFail:
			case HeadImageMgr.HeadImgStatus.noImg:
				return false;
			default:
				return false;
			}
		}
		public static void setHasHeadImage(string strUsrName, bool bHasImg)
		{
			if (string.IsNullOrEmpty(strUsrName))
			{
				return;
			}
			HeadImageMgr.mapHeadImgStatus.set_Item(strUsrName, bHasImg ? HeadImageMgr.HeadImgStatus.hasImg : HeadImageMgr.HeadImgStatus.noImg);
		}
		public static void setHeadImageVersion(string strUsrName, int version)
		{
			if (!string.IsNullOrEmpty(strUsrName) && version != 0)
			{
				HeadImageMgr.mapHeadImgVersion.set_Item(strUsrName, version);
				return;
			}
		}
		public static void cancel()
		{
			ServiceCenter.sceneBatchGetHeadImg.cancel();
		}
		private static void downLoadRequest(string userName)
		{
			HeadImageMgr.downLoadRequestList.Add(userName);
			if (HeadImageMgr.downLoadRequestList.get_Count() == 1)
			{
				Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
				{
					ServiceCenter.sceneBatchGetHeadImg.doScene(HeadImageMgr.downLoadRequestList);
					HeadImageMgr.downLoadRequestList.Clear();
				});
			}
		}
		private static void addCallback(getImageCallBack getImageCallBack_0, string userName)
		{
			if (!HeadImageMgr.mapHeadImgCallBack.ContainsKey(getImageCallBack_0))
			{
				HeadImageMgr.mapHeadImgCallBack.set_Item(getImageCallBack_0, new List<string>());
			}
			List<string> list = HeadImageMgr.mapHeadImgCallBack.get_Item(getImageCallBack_0);
			if (list.Contains(userName))
			{
				return;
			}
			list.Add(userName);
		}
		private static int InvokedCallBack(string userName, BitmapImage bitmapImage_0)
		{
			HeadImageMgr.cbList.Clear();
			using (Dictionary<getImageCallBack, List<string>>.Enumerator enumerator = HeadImageMgr.mapHeadImgCallBack.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					KeyValuePair<getImageCallBack, List<string>> current = enumerator.get_Current();
					if (current.get_Value().Contains(userName))
					{
						HeadImageMgr.cbList.Add(current.get_Key());
						current.get_Value().Remove(userName);
					}
				}
			}
			using (List<getImageCallBack>.Enumerator enumerator2 = HeadImageMgr.cbList.GetEnumerator())
			{
				while (enumerator2.MoveNext())
				{
					getImageCallBack current2 = enumerator2.get_Current();
					current2(userName, bitmapImage_0);
					if (HeadImageMgr.mapHeadImgCallBack.get_Item(current2).get_Count() <= 0)
					{
						HeadImageMgr.mapHeadImgCallBack.Remove(current2);
					}
				}
			}
			return Enumerable.Count<getImageCallBack>(HeadImageMgr.cbList);
		}
		public static bool smethod_0(string usrName, bool isContact = true)
		{
			if (string.IsNullOrEmpty(usrName))
			{
				return false;
			}
			StorageIO.deleteFile(HeadImageMgr.getPath(usrName, isContact));
			return true;
		}
		public static string getPath(string strUserName, bool isContact = true)
		{
			if (isContact)
			{
				return StorageIO.getHeadImgPath() + "/" + strUserName + ".jpg";
			}
			return HeadImageMgr.hasher.getHashPath(strUserName);
		}
		public static bool exists(string strUserName, bool isContact = true)
		{
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					return userStoreForApplication.FileExists(HeadImageMgr.getPath(strUserName, isContact));
				}
			}
			catch (Exception)
			{
			}
			return false;
		}
		private static void writeHeadImg(string strUserName, byte[] imageBuf)
		{
			if (HeadImageMgr.isContact(strUserName))
			{
				StorageIO.writeToFile(StorageIO.getHeadImgPath() + "/" + strUserName + ".jpg", imageBuf, false);
				return;
			}
			if (HeadImageMgr.mapHeadImgVersion.ContainsKey(strUserName))
			{
				int num = HeadImageMgr.mapHeadImgVersion.get_Item(strUserName);
				HeadImageMgr.mapHeadImgLocalVersion.set_Item(strUserName, num);
				HeadImageMgr.hasher.write(strUserName, new byte[][]
				{
					imageBuf,
					BitConverter.GetBytes(num),
					HeadImageMgr.versionTag
				});
				return;
			}
			HeadImageMgr.hasher.write(strUserName, new byte[][]
			{
				imageBuf
			});
		}
		private static byte[] readHeadImg(string strUserName)
		{
			if (HeadImageMgr.isContact(strUserName))
			{
				return StorageIO.readFromFile(StorageIO.getHeadImgPath() + "/" + strUserName + ".jpg");
			}
			return HeadImageMgr.hasher.read(strUserName);
		}
		private static int getHeadImgVersion(byte[] imageBuf)
		{
			if (imageBuf == null || imageBuf.Length <= 11)
			{
				return -1;
			}
			byte[] array = new byte[7];
			Array.Copy(imageBuf, imageBuf.Length - 7, array, 0, 7);
			if (!Util.byteArrayEqual(array, HeadImageMgr.versionTag))
			{
				return -1;
			}
			return BitConverter.ToInt32(imageBuf, imageBuf.Length - 11);
		}
		public static bool isHeadImgVersionChange(string strUserName)
		{
			return HeadImageMgr.mapHeadImgVersion.ContainsKey(strUserName) && HeadImageMgr.mapHeadImgLocalVersion.ContainsKey(strUserName) && HeadImageMgr.mapHeadImgVersion.get_Item(strUserName) != HeadImageMgr.mapHeadImgLocalVersion.get_Item(strUserName);
		}
		public static void save(string usrName, byte[] imageBuf)
		{
			if (string.IsNullOrEmpty(usrName) || imageBuf == null || imageBuf.Length <= 0)
			{
				return;
			}
			if (usrName == AccountMgr.strUsrName)
			{
				AccountMgr.hasHeadImg = true;
			}
			if (!HeadImageMgr.mapHeadImgStatus.ContainsKey(usrName) && !HeadImageMgr.mapHeadImg.ContainsKey(usrName) && !(usrName == AccountMgr.strUsrName))
			{
				HeadImageMgr.writeHeadImg(usrName, imageBuf);
				return;
			}
			BitmapImage bitmapImage = ImageCacheMgr.loadFromBuf(imageBuf);
			if (bitmapImage == null)
			{
				HeadImageMgr.mapHeadImgStatus.set_Item(usrName, HeadImageMgr.HeadImgStatus.decodeFail);
				Log.smethod_1("HeadImageMgr", "loadImage decode imgbuf fail " + usrName + " 0x" + Util.toHexString(imageBuf, 4));
				return;
			}
			HeadImageMgr.mapHeadImgStatus.set_Item(usrName, HeadImageMgr.HeadImgStatus.hasImg);
			HeadImageMgr.addCache(usrName, bitmapImage);
			HeadImageMgr.writeHeadImg(usrName, imageBuf);
			if (HeadImageMgr.InvokedCallBack(usrName, bitmapImage) == 0)
			{
				EventCenter.postCombineEvent(EventConst.ON_MGR_HEAD_IMG_READY, usrName);
			}
			HeadImageMgr.tryCleanHeadImage();
		}
		public static void save(string usrName, Stream fileStream)
		{
			if (string.IsNullOrEmpty(usrName) || fileStream == null)
			{
				return;
			}
			byte[] array = Util.toJpeg(fileStream, 132, 132, 70);
			BitmapImage bitmapImage = ImageCacheMgr.loadFromBuf(array);
			if (array != null && bitmapImage != null)
			{
				if (usrName == AccountMgr.strUsrName)
				{
					AccountMgr.hasHeadImg = true;
				}
				HeadImageMgr.mapHeadImgStatus.set_Item(usrName, HeadImageMgr.HeadImgStatus.hasImg);
				HeadImageMgr.addCache(usrName, bitmapImage);
				HeadImageMgr.writeHeadImg(usrName, array);
				EventCenter.postCombineEvent(EventConst.ON_MGR_HEAD_IMG_READY, usrName);
				return;
			}
			Log.smethod_1("HeadImageMgr", "save img to jpg encode  fail " + usrName);
		}
		public static void update(string usrName)
		{
			if (string.IsNullOrEmpty(usrName))
			{
				return;
			}
			if (Enumerable.Contains<string>(HeadImageMgr.ResHeadImageUserNames, usrName))
			{
				return;
			}
			Log.smethod_5("HeadImageMgr", "updateHeadImg=" + usrName);
			StorageMgr.hdheadimg.method_0(usrName);
			if (usrName == AccountMgr.strUsrName)
			{
				AccountMgr.hasHeadImg = true;
				NetSceneBatchGetHeadImg arg_5E_0 = ServiceCenter.sceneBatchGetHeadImg;
				List<string> list = new List<string>();
				list.Add(usrName);
				arg_5E_0.doScene(list);
				return;
			}
			HeadImageMgr.smethod_0(usrName, true);
			if (HeadImageMgr.mapHeadImgStatus.ContainsKey(usrName) && HeadImageMgr.mapHeadImgStatus.get_Item(usrName) == HeadImageMgr.HeadImgStatus.noImg)
			{
				HeadImageMgr.mapHeadImgStatus.set_Item(usrName, HeadImageMgr.HeadImgStatus.hasImg);
			}
			if (!HeadImageMgr.mapHeadImg.ContainsKey(usrName))
			{
				return;
			}
			NetSceneBatchGetHeadImg arg_B5_0 = ServiceCenter.sceneBatchGetHeadImg;
			List<string> list2 = new List<string>();
			list2.Add(usrName);
			arg_B5_0.doScene(list2);
		}
		public static void getImageBroadCasting(string usrName, BitmapImage bitmapImage_0)
		{
			EventCenter.postCombineEvent(EventConst.ON_MGR_HEAD_IMG_READY, usrName);
		}
		private static void tryCleanHeadImage()
		{
			int nowDays = Util.getNowDays();
			if (ConfigMgr.getInt("HeadImageMgrCleanHeadImg", 0) == nowDays)
			{
				return;
			}
			Log.smethod_5("HeadImageMgr", "clean time days=" + Util.getNowDays());
			ConfigMgr.setInt("HeadImageMgrCleanHeadImg", nowDays);
			HeadImageMgr.CleanThreadParam cleanThreadParam = new HeadImageMgr.CleanThreadParam();
			cleanThreadParam.strUsrName = AccountMgr.strUsrName;
			cleanThreadParam.path = StorageIO.getHeadImgTempPath();
			cleanThreadParam.index = ConfigMgr.getInt("HeadImageMgrCleanDirIndex", 0);
			Thread thread = new Thread(new ParameterizedThreadStart(HeadImageMgr.CleanHeadImgThread));
			thread.set_Name("HeadImageCleanThread");
			thread.set_IsBackground(true);
			thread.Start(cleanThreadParam);
		}
		private static void CleanHeadImgThread(object object_0)
		{
			HeadImageMgr.CleanThreadParam param = object_0 as HeadImageMgr.CleanThreadParam;
			if (param == null)
			{
				return;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					DebugEx.getTimeSpan();
					Log.smethod_4("HeadImageMgr", "CleanHeadImg begin");
					DateTime now = DateTime.get_Now();
					int int_0;
					for (int_0 = 0; int_0 < 26; int_0++)
					{
						string text = param.path + "/" + (param.index + int_0) % 26;
						if (userStoreForApplication.DirectoryExists(text))
						{
							HeadImageMgr.cleanDir(userStoreForApplication, text, param.strUsrName + "@bottle");
							if ((DateTime.get_Now() - now).get_TotalSeconds() >= 4.0)
							{
								int_0++;
								IL_F7:
								Log.smethod_4("HeadImageMgr", string.Concat(new object[]
								{
									"CleanHeadImg end,",
									param.strUsrName,
									",time=",
									DebugEx.getTimeSpan()
								}));
								Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
								{
									if (AccountMgr.isLogin && !(AccountMgr.strUsrName != param.strUsrName))
									{
										ConfigMgr.setInt("HeadImageMgrCleanDirIndex", (param.index + int_0) % 26);
										return;
									}
								});
								goto IL_167;
							}
						}
					}
					goto IL_F7;
				}
				IL_167:;
			}
			catch (Exception ex)
			{
				Log.smethod_1("HeadImageMgr", "cleanDir fail " + ex);
			}
		}
		public static void cleanDir(IsolatedStorageFile isolatedStorageFile_0, string path, string skipFileName)
		{
			string[] fileNames = isolatedStorageFile_0.GetFileNames(path + "/*");
			if (Enumerable.Count<string>(fileNames) >= 300)
			{
				int num = 0;
				string[] array = fileNames;
				for (int i = 0; i < array.Length; i++)
				{
					string text = array[i];
					try
					{
						if (!(text == skipFileName))
						{
							DateTimeOffset lastAccessTime = isolatedStorageFile_0.GetLastAccessTime(path + "/" + text);
							if (Math.Abs((DateTime.get_Now() - lastAccessTime).get_TotalDays()) > 7.0)
							{
								isolatedStorageFile_0.DeleteFile(path + "/" + text);
								num++;
								isolatedStorageFile_0.DeleteFile(StorageIO.getHeadImgHDPath() + "/" + text);
							}
						}
					}
					catch
					{
					}
				}
				Log.smethod_4("HeadImageMgr", string.Concat(new object[]
				{
					"CleanHeadImg del file in dir=",
					path,
					",del count=",
					num
				}));
			}
		}
	}
}
